﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using RichECMS;
using Common;

namespace RichECMS.ServerPages.CoderEditor
{
    public partial class CodeEditor : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (State.LoginUser.ManagerCompetence > (int)Utility.ManagerCompetence.Designer)
            {
                Response.Write("您没有权限查看改页面");
                Response.End();
            }

            if (Request["filepath"] != null)
            {
                this.code.InnerHtml = GetFileStream(Server.UrlDecode(Request["filepath"].ToString()));
            }
        }

        public override void Alert(string strMsg)
        {
            this.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "msg", "<script>alert(\"" + strMsg.Replace("\r\n", "") + "\")</script>");
        }

        private string GetFileStream(string filepath)
        {string strEcoding="";
            StringBuilder result = new StringBuilder();
            if (File.Exists(State.LoginUser.Rootpath + filepath))
            {
                using (FileStream fs = new FileStream(State.LoginUser.Rootpath + filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    FileStream fsGetEncodeing = new FileStream(State.LoginUser.Rootpath + filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    Util.IdentifyEncoding.IdentifyEncoding uEncoding=new Util.IdentifyEncoding.IdentifyEncoding();
                    strEcoding = uEncoding.GetEncodingName(fsGetEncodeing);
                    fsGetEncodeing.Close();
                    StreamReader sr = new StreamReader(fs,true);
                    result.Append(sr.ReadToEnd());
                    sr.Close();
                }
                if (strEcoding != "UTF-8")
                {
                    Alert("该文件编码是" + strEcoding + "; 不是默认的 utf-8 格式,打开后中文可能会有乱码，请注意");
                }
            }
            else
            {
                Alert("这个文件不存在");
            }
            return result.ToString();
        }

        public string GetCodeType()
        {
            if (Request["filepath"] == null)
            {
                return "";
            }
            var filepath = Server.UrlDecode(Request["filepath"].ToString());
            string extenName = filepath.Remove(0, filepath.LastIndexOf('.')+1).ToLower().Trim();
            string result = "";
            switch (extenName)
            {
                case "css":
                    result = "css";
                    break;
                case "js":
                    result = "javascript";
                    break;
                case "aspx":
                    result = "htmlembedded";
                    break;
                case "cs":
                    result = "clike";
                    break;
                case "ashx":
                    result = "clike";
                    break;
                case "htm":
                    result = "htmlmixed";
                    break;
                case "html":
                    result = "htmlmixed";
                    break;
                case "shtml":
                    result = "htmlmixed";
                    break;
                case "xml":
                    result = "xml";
                    break;
                case "php":
                    result = "php";
                    break;
            }
            return result;
        }
    }
}